<?php
class Prize_every_day_model extends BZ_Model
{
    protected $_table = 'prize_every_day';

    /**
     * 查找
     * @param $params
     * @return mixed
     */
    public function find($params){
        $this->db->select('day_id,prize_id,date,quantity,surplus_quantity,probability,status,created_at,updated_at, DAY(`date`) AS "day", winners_user, start_time, end_time');
        $this->db->from($this->_table);
        if (!empty($params['date'])){
            if (is_array($params['date'])){
                $this->db->where_in('date', $params['date']);
            } else {
                $this->db->like('date', $params['date'], 'after');
            }
        }
        $this->db->where('prize_id', $params['prize_id']);
        $this->db->order_by('day', ' ASC');
        return $this->db->get();
    }

    /**
     * 批量添加
     * @param $postData
     * @param $prizeId
     * @return mixed
     */
    public function insertBatch($postData, $dates){
        $data = array();
        foreach ($dates as $date=>$value){
            $data[] = array(
                'prize_id'         => $postData['prize_id'],
                'date'             => $date,
                'quantity'         => $postData['quantity'],
                'surplus_quantity' => $postData['quantity'],
                'probability'      => empty($postData['probability']) ? 0 : $postData['probability'],
                'status'           => $postData['status'],
                'winners_user'      => '',
                'time_slot'        => '',
                'end_time'         => '',
                'start_time'       => '',
                'created_at'       => date('Y-m-d H:i:s'),
                'updated_at'       => '0000-00-00 00:00:00'
            );
        }
        return $this->db->insert_batch($this->_table, $data);
    }

    /**
     * 批量修改
     * @param $updateData
     * @param $postData
     * @return bool
     */
    public function updateBatch($updateData, $postData){
        $data = array();
        foreach ($updateData as $key=>$value){
            $data[] = array(
                'day_id'           => $value['day_id'],
                'quantity'         => $postData['quantity'],
                'surplus_quantity' => $postData['quantity'],
                'probability'      => empty($postData['probability']) ? 0 : $postData['probability'],
                'status'           => $postData['status']
            );
        }
        $this->db->where('prize_id', $postData['prize_id']);
        return $this->db->update_batch($this->_table, $data, 'day_id');
    }

    /**
     * 根据奖品id 删除
     */
    public function deletePrizeEveryDay($prizeId)
    {
        return $this->db->delete($this->_table, array('prize_id'=>$prizeId));
    }

}